<?php
namespace app\common\controller;

/**
 * 发送消息类
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/28
 * Time: 17:12
 */
class SendMsg{

    /**
     * 发送事项消息
     * @param $openid
     * @param $task
     * @param $username
     * @param $tel
     * @param $remark
     * @return mixed
     */
    public function send_matter_msg($openid,$task,$username,$tel,$remark){
        $maps = [];
        $maps[] = ['openid','=',$openid];
        $maps[] = ['status','=',1];
        $maps[] = ['enable','=',1];
        $u_detail = model('user')->get_detail($maps,'id');
        if(empty($u_detail)){
            return 0;
        }

        $maps = [];
        $maps[] = ['key','in',['access_token','wx_msg_template']];
        $conf_info = model('conf')->get_list($maps);
        $conf_info_new = [];
        foreach ($conf_info as $k => $v) {
            $conf_info_new[$v['key']] = $v['value'];
        }

        $access_token_arr = json_decode($conf_info_new['access_token'],true);
        $wx_msg_template_arr = json_decode($conf_info_new['wx_msg_template'],true);

        $tools = new Tools();
        $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=' . $access_token_arr['access_token'];

        $post_data['touser'] = $openid;

        $mp_template_msg['appid'] = \think\facade\Config::get('xcx.gzh_appid');
        $mp_template_msg['template_id'] = $wx_msg_template_arr['matter_template'];
        $mp_template_msg['url'] = 'http://www.tyll.net.cn';

        $miniprogram['appid'] = \think\facade\Config::get('xcx.xcx_appid');
        $miniprogram['path'] =  '/pages/home/home';
        $mp_template_msg['miniprogram'] = $miniprogram;

        $data['first']['value'] = '您有一个事项更新，请查看';
        $data['first']['color'] = '#000000';
        $data['keyword1']['value'] = $task;
        $data['keyword1']['color'] = '#FF0000';
        $data['keyword2']['value'] = $username;
        $data['keyword2']['color'] = '#FF0000';
        $data['keyword3']['value'] = $tel;
        $data['keyword3']['color'] = '#FF0000';
        $data['remark']['value'] = $remark;
        $data['remark']['color'] = '#FF0000';
        $mp_template_msg['data'] = $data;
        $post_data['mp_template_msg'] = $mp_template_msg;
        //var_dump(json_encode($post_data,JSON_UNESCAPED_UNICODE));exit;

        $r_json = $tools->PostuseCurl($url,json_encode($post_data));
        $r_arr = json_decode($r_json,true);
        return $r_arr;
    }

    /**
     * 发送抄送消息
     * @param $openid
     * @param $task
     * @param $username
     * @param $tel
     * @param $remark
     * @return mixed
     */
    public function send_copy_msg($openid,$task,$username,$tel,$remark){
        $maps = [];
        $maps[] = ['key','in',['access_token','wx_msg_template']];
        $conf_info = model('conf')->get_list($maps);
        $conf_info_new = [];
        foreach ($conf_info as $k => $v) {
            $conf_info_new[$v['key']] = $v['value'];
        }

        $access_token_arr = json_decode($conf_info_new['access_token'],true);
        $wx_msg_template_arr = json_decode($conf_info_new['wx_msg_template'],true);

        $tools = new Tools();
        $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=' . $access_token_arr['access_token'];

        $post_data['touser'] = $openid;

        $mp_template_msg['appid'] = \think\facade\Config::get('xcx.gzh_appid');
        $mp_template_msg['template_id'] = $wx_msg_template_arr['copy_template'];
        $mp_template_msg['url'] = 'http://www.tyll.net.cn';

        $miniprogram['appid'] = \think\facade\Config::get('xcx.xcx_appid');
        $miniprogram['path'] =  '/pages/home/home';
        $mp_template_msg['miniprogram'] = $miniprogram;

        $data['first']['value'] = '您有一个抄送更新，请查看';
        $data['first']['color'] = '#000000';
        $data['keyword1']['value'] = $task;
        $data['keyword1']['color'] = '#FF0000';
        $data['keyword2']['value'] = $username;
        $data['keyword2']['color'] = '#FF0000';
        $data['keyword3']['value'] = $tel;
        $data['keyword3']['color'] = '#FF0000';
        $data['remark']['value'] = $remark;
        $data['remark']['color'] = '#FF0000';
        $mp_template_msg['data'] = $data;
        $post_data['mp_template_msg'] = $mp_template_msg;
        //var_dump(json_encode($post_data,JSON_UNESCAPED_UNICODE));exit;

        $r_json = $tools->PostuseCurl($url,json_encode($post_data));
        $r_arr = json_decode($r_json,true);
        return $r_arr;
    }

    /**
     * 发送批复消息
     * @param $openid
     * @param $task
     * @param $username
     * @param $tel
     * @param $remark
     * @return mixed
     */
    public function send_reply_msg($openid,$task,$username,$tel,$remark){
        $maps = [];
        $maps[] = ['key','in',['access_token','wx_msg_template']];
        $conf_info = model('conf')->get_list($maps);
        $conf_info_new = [];
        foreach ($conf_info as $k => $v) {
            $conf_info_new[$v['key']] = $v['value'];
        }

        $access_token_arr = json_decode($conf_info_new['access_token'],true);
        $wx_msg_template_arr = json_decode($conf_info_new['wx_msg_template'],true);

        $tools = new Tools();
        $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=' . $access_token_arr['access_token'];

        $post_data['touser'] = $openid;

        $mp_template_msg['appid'] = \think\facade\Config::get('xcx.gzh_appid');
        $mp_template_msg['template_id'] = $wx_msg_template_arr['reply_template'];
        $mp_template_msg['url'] = 'http://www.tyll.net.cn';

        $miniprogram['appid'] = \think\facade\Config::get('xcx.xcx_appid');
        $miniprogram['path'] =  '/pages/home/home';
        $mp_template_msg['miniprogram'] = $miniprogram;

        $data['first']['value'] = '您有一个批复更新，请查看';
        $data['first']['color'] = '#000000';
        $data['keyword1']['value'] = $task;
        $data['keyword1']['color'] = '#FF0000';
        $data['keyword2']['value'] = $username;
        $data['keyword2']['color'] = '#FF0000';
        $data['keyword3']['value'] = $tel;
        $data['keyword3']['color'] = '#FF0000';
        $data['remark']['value'] = $remark;
        $data['remark']['color'] = '#FF0000';
        $mp_template_msg['data'] = $data;
        $post_data['mp_template_msg'] = $mp_template_msg;
        //var_dump(json_encode($post_data,JSON_UNESCAPED_UNICODE));exit;

        $r_json = $tools->PostuseCurl($url,json_encode($post_data));
        $r_arr = json_decode($r_json,true);
        return $r_arr;
    }

    /**
     * 发送工位留言消息
     * @param $openid
     * @param $username
     * @param $tel
     * @param $msg
     * @param $c_time
     * @param $wsid
     * @param $ws_str
     * @return mixed
     */
    public function send_wsm_msg($openid,$username,$tel,$msg,$c_time,$wsid,$ws_str){
        $maps = [];
        $maps[] = ['key','in',['access_token','wx_msg_template']];
        $conf_info = model('conf')->get_list($maps);
        $conf_info_new = [];
        foreach ($conf_info as $k => $v) {
            $conf_info_new[$v['key']] = $v['value'];
        }

        $access_token_arr = json_decode($conf_info_new['access_token'],true);
        $wx_msg_template_arr = json_decode($conf_info_new['wx_msg_template'],true);

        $tools = new Tools();
        $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=' . $access_token_arr['access_token'];

        $post_data['touser'] = $openid;

        $mp_template_msg['appid'] = \think\facade\Config::get('xcx.gzh_appid');
        $mp_template_msg['template_id'] = $wx_msg_template_arr['wsm_template'];
        $mp_template_msg['url'] = 'http://www.tyll.net.cn';

        $miniprogram['appid'] = \think\facade\Config::get('xcx.xcx_appid');
        $miniprogram['pagepath'] =  'pages/patrol-shop/patrol-space/patrol_space?spaceid=' . $wsid;
        $mp_template_msg['miniprogram'] = $miniprogram;

        $data['first']['value'] = '您好，有新的客户对工位/办公室留言';
        $data['first']['color'] = '#000000';
        $data['keyword1']['value'] = '工位/办公室小程序码进入';
        $data['keyword1']['color'] = '#FF0000';
        $data['keyword2']['value'] = $username;
        $data['keyword2']['color'] = '#FF0000';
        $data['keyword3']['value'] = $tel;
        $data['keyword3']['color'] = '#FF0000';
        $data['keyword4']['value'] = $ws_str;
        $data['keyword4']['color'] = '#FF0000';
        $data['keyword5']['value'] = $c_time;
        $data['keyword5']['color'] = '#FF0000';
        $data['remark']['value'] = $msg;
        $data['remark']['color'] = '#FF0000';
        $mp_template_msg['data'] = $data;
        $post_data['mp_template_msg'] = $mp_template_msg;
        //var_dump(json_encode($post_data,JSON_UNESCAPED_UNICODE));exit;

        $r_json = $tools->PostuseCurl($url,json_encode($post_data));
        $r_arr = json_decode($r_json,true);
        return $r_arr;
    }
}